home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mint99s / syscall.spp < prev    next >
Text File  |  1993-01-20  |  12KB  |  429 lines

  1. ; Copyright 1992 Eric R. Smith
  2. ; Copyright 1992 Atari Corporation
  3. ; All rights reserved.
  4.  
  5. %include "magic.i"
  6. ;
  7. ; syscall: interface for system calls. The following entry points are
  8. ;    defined:
  9. ; _mint_bios:  entry point for the BIOS calls (trap #13)
  10. ; _mint_xbios: entry point for XBIOS calls (trap #14)
  11. ; _mint_dos:   entry point for GEMDOS calls (trap #1)
  12. ; _sig_return: user signal handlers return to this routine (see signal.c)
  13. ;              it is responsible for restoring the kernel's old context
  14. ;              via the Psigreturn() system call
  15. ; _lineA0:     calls the line A initialize routine
  16. ; _call_aes:   calls the GEM AES
  17. ; _callout:    calls an external function, after first saving all registers,
  18. ;              and restores the registers afterward
  19. ;
  20. ; external variables referenced:
  21. ; _bios_tab, _bios_max:
  22. ;    table of entry points for BIOS routines, max # of routine
  23. ; _xbios_tab, _xbios_max:
  24. ;    ditto for XBIOS
  25. ; _dos_tab, _dos_max:
  26. ;    ditto for GEMDOS
  27. ; _curproc:
  28. ;    pointer to current process table entry, and hence to save area for
  29. ;    context (this is always the first entry in the PROC table).
  30. ; _valid_return:
  31. ;    used to indicate to the kernel that a valid return from user mode
  32. ;    is taking place
  33. ;
  34. ; _bconbuf, _bconbsiz, _bconbdev:
  35. ;    256 byte buffer for Bconout() output. If _bconbsiz is non-zero,
  36. ;    there are that many bytes in _bconbuf waiting to be flushed. The
  37. ;    output is for device _bconbdev.
  38. ;
  39. ; The C function enter_kernel() is called on entry to the kernel, and the
  40. ; function leave_kernel() is called on exit. These functions are responsible
  41. ; for saving and restoring the various trap vectors, so that MiNT can trap
  42. ; out to TOS directly, but programs can only trap to MiNT.
  43. ;
  44. ; we also call certain BIOS functions directly if these are known not to
  45. ; require saving/restoring of context
  46. ;
  47.     TEXT
  48.     
  49.     XDEF    _mint_bios,_mint_xbios
  50.     XDEF    _mint_dos
  51.     XREF    _build_context
  52.     XREF    _restore_context
  53.     XREF    _proc_clock        ; controls process' allocation of CPU time
  54.     XREF    _enter_kernel
  55.     XREF    _leave_kernel
  56.     XREF    _preempt
  57.  
  58.     XREF    _curproc
  59.     XREF    _bios_tab,_bios_max
  60.     XREF    _xbios_tab,_xbios_max
  61.     XREF    _dos_tab,_dos_max
  62.  
  63.     XREF    _bconbuf,_bconbsiz,_bconbdev
  64.     XREF    _bflush
  65.  
  66.     XREF    _ubconstat,_do_bconin,_ubcostat,_kbshift
  67.     
  68. _mint_dos:
  69.     clr.w    -(sp)            ; no frame format needed
  70.     move.l    _curproc,d0
  71.     addq.l    #4,d0
  72.     move.l    d0,-(sp)        ; push pointer to syscall context save
  73.     jsr    _build_context
  74.     move.l    #_dos_tab,a5        ; set syscall_tab
  75.     move.w    _dos_max,d5        ; set syscall_max
  76. ;
  77. ; copy parameters onto process stack. a0 and a1 were set by _build_context
  78. ;
  79.  
  80.     move.l    _curproc,a0
  81.     move.l    (a0),sp            ; this puts us in our private stack
  82.     move.l    24(a1),-(sp)        ; a1 was set by build_context
  83.     move.l    20(a1),-(sp)
  84.     move.l    16(a1),-(sp)
  85.     move.l    12(a1),-(sp)
  86.     move.l    8(a1),-(sp)
  87.     move.l    4(a1),-(sp)
  88.     move.l    (a1),-(sp)
  89.     jsr    _enter_kernel        ; set up vectors appropriately
  90.     bra    _syscall
  91.  
  92. _mint_xbios:
  93. ;
  94. ; Kludge for Setscreen: under Falcon TOS, this
  95. ; calls a GEMDOS function (Srealloc) and so
  96. ; we must *not* change any of the vectors!!
  97.  
  98.     btst    #13,(sp)        ; test for user/super mode
  99.     beq    LX_usr
  100. %ifdef ONLY030
  101.     lea    8(sp),a1
  102. %else
  103.     lea    6(sp),a1        ; supervisor mode: args on stack
  104.     tst.w    ($59e).w        ; test longframe
  105.     beq.s    LX_check
  106.     addq.l    #2,a1            ; stack is a bit bigger
  107. %endif
  108.     bra.s    LX_check
  109. LX_usr:
  110.     move.l    usp,a1            ; user mode: args on user stack
  111. LX_check:
  112.     move.w    (a1),d0            ; check command
  113.     cmp.w    #5,d0            ; Setscreen?
  114.     beq    LX_screen        ; no -- fall through
  115.  
  116.     clr.w    -(sp)            ; no frame format needed
  117.     move.l    _curproc,d0
  118.     addq.l    #4,d0
  119.     move.l    d0,-(sp)        ; push pointer to syscall context save
  120.     jsr    _build_context
  121.     move.l    #_xbios_tab,a5        ; set syscall_tab
  122.     move.w    _xbios_max,d5        ; set syscall_max
  123. ;
  124. ; copy parameters onto process stack. a0 and a1 were set by _build_context
  125. ;
  126.  
  127.     move.l    _curproc,a0
  128.     move.l    (a0),sp            ; this puts us in our private stack
  129.     move.l    24(a1),-(sp)        ; a1 was set by build_context
  130.     move.l    20(a1),-(sp)
  131.     move.l    16(a1),-(sp)
  132.     move.l    12(a1),-(sp)
  133.     move.l    8(a1),-(sp)
  134.     move.l    4(a1),-(sp)
  135.     move.l    (a1),-(sp)
  136.     jsr    _enter_kernel        ; set up vectors appropriately
  137.     bra    _syscall
  138. ;
  139. ; For setscreen, jump directly to the ROM vector --
  140. ; this avoids all hazards (like changing the vectors)
  141.  
  142. LX_screen:
  143.     move.l    _old_xbios+8,a0
  144.     jmp    (a0)
  145.  
  146.  
  147. _mint_bios:
  148. ;
  149. ; Entering the kernel can be very expensive; so, we take a short-cut
  150. ; if possible -- we try some BIOS functions out, and if they
  151. ; succeed without blocking then we're done; otherwise, we go
  152. ; through the long procedure for entering the kernel
  153. ;
  154. ; These shortcuts aren't done when we're called in supervisor mode,
  155. ; because TOS uses very tiny stacks (smaller than we want); the
  156. ; shortcuts operate on the user-supplied ssp, whereas the "full"
  157. ; BIOS code works on our (private) system stack
  158. ;
  159. ; the shortcuts are also turned off if BIOSBUF=n
  160. ;
  161.     jsr    _enter_kernel        ; set up BIOS vectors
  162.     tst.w    _bconbdev        ; is BIOS buffering on?
  163.     bmi    L_bios            ; no; skip all this
  164.  
  165.     btst    #5,(sp)            ; test for user/super mode
  166.     bne.s    L_bios            ; if super, goto L_bios
  167.     tst.w    _proc_clock        ; are we about to be preempted?
  168.     beq    L_bios
  169.  
  170.     move.l    usp,a1            ; user mode: args on user stack
  171. L_ubios:
  172.     move.w    (a1),d0            ; get command
  173.     cmp.w    #3,d0            ; Bconout?
  174.     beq    do_bconout        ; yes -- go do it
  175. ;
  176. ; most of the remaining functions require BIOS vectors to be properly
  177. ; set up
  178.     tst.w    _bconbsiz        ; is BIOS output waiting?
  179.     bne.s    L_bios            ; yes -- do regular code
  180.  
  181. ; test for various BIOS functions
  182.     cmp.w    #1,d0            ; Bconstat?
  183.     bne.s    L_00
  184.     move.w    2(a1),-(sp)        ; push arg
  185.     jsr    _ubconstat
  186. L_1out:
  187.     addq.l    #2,sp
  188. L_0out:
  189.     move.l    d0,-(sp)        ; save d0
  190.     ori.w    #$0700,sr        ; spl7()
  191.     jsr    _leave_kernel
  192.     move.l    (sp)+,d0        ; retrieve value to be returned
  193.     rte                ; return to user
  194. L_00:
  195.     cmp.w    #2,d0            ; Bconin?
  196.     bne.s    L_01
  197.     move.w    2(a1),-(sp)        ; yes; push argument
  198.     jsr    _do_bconin
  199.     addq.l    #2,sp
  200.     cmp.w    #$dead,d0        ; would Bconin block?
  201.     bne.s    L_0out            ; no -- we're done
  202.     bra.s    L_bios            ; yes -- do the long stuff
  203. L_01:
  204.     cmp.w    #8,d0            ; Bcostat?
  205.     bne.s    L_02
  206.     move.w    2(a1),-(sp)        ; push device
  207.     jsr    _ubcostat        ; get status
  208.     bra.s    L_1out
  209. L_02:
  210.     cmp.w    #11,d0            ; Kbshift?
  211.     bne.s    L_bios
  212.     move.w    2(a1),-(sp)        ; push arg
  213.     jsr    _kbshift
  214.     bra.s    L_1out
  215.  
  216. L_bios:
  217.     clr.w    -(sp)            ; no frame format needed
  218.     move.l    _curproc,d0
  219.     addq.l    #4,d0
  220.     move.l    d0,-(sp)        ; push pointer to syscall context save
  221.     jsr    _build_context
  222.     move.l    #_bios_tab,a5        ; set syscall_tab
  223.     move.w    _bios_max,d5        ; set syscall_max
  224. ;
  225. ; copy parameters onto process stack. a0 and a1 were set by _build_context
  226. ;
  227.  
  228.     move.l    _curproc,a0
  229.     move.l    (a0),sp            ; this puts us in our private stack
  230.     move.l    24(a1),-(sp)        ; a1 was set by build_context
  231.     move.l    20(a1),-(sp)
  232.     move.l    16(a1),-(sp)
  233.     move.l    12(a1),-(sp)
  234.     move.l    8(a1),-(sp)
  235.     move.l    4(a1),-(sp)
  236.     move.l    (a1),-(sp)
  237.  
  238. _syscall:
  239. ;
  240. ; check here to see if we need to flush the Bconout() buffer
  241. ;
  242.     tst.w    _bconbsiz        ; characters in buffer?
  243.     beq.s    L_noflush        ; no: OK to proceed
  244. ;
  245. ; watch out, this could cause a context switch
  246. ;
  247.     jsr    _bflush            ; flush the buffer
  248.  
  249. L_noflush:
  250. ;
  251. ; figure out which routine to call
  252. ;
  253.     move.w    (sp),d0
  254.     cmp.w    #-1,d0            ; trapping with -1 means return
  255.     bne.s    check_max        ; the corresponding system table
  256.     move.l    a5,d0
  257.     bra.s    out
  258. check_max:
  259.     cmp.w    d5,d0
  260.     bge.s    error
  261.     add.w    d0,d0
  262.     add.w    d0,d0            ; multiply by 4
  263.     move.l    0(a5,d0.w),d0        ; d0 = syscall_tab[d0]
  264.     beq.s    error            ; null entry means invalid call
  265.     addq.l    #2,sp            ; pop function number off stack
  266.     move.l    d0,a0
  267.     jsr    (a0)            ; go do the call
  268. out:
  269.     move.l    _curproc,a0
  270.     move.l    d0,P_SYSCTXT+C_D0(a0)    ; set d0 in the saved context
  271.     move.w    P_SYSCTXT+C_SR(a0),d0    ; get saved status register
  272.     
  273.     tst.l    P_PTRACER(a0)        ; check curproc->ptracer, if not set
  274.     beq.s    notrace            ; then no pending trace; this ensures
  275.     move.w    d0,d1            ; we work with non-MiNT debuggers
  276.     and.w    #$c000,d1        ; are either of the trace bits set
  277.     sne    P_SYSCTXT+C_PTRACE(a0)    ; mark as trace pending/not
  278. notrace:
  279.     tst.w    _proc_clock        ; has process exceeded time slice?
  280.     bne.s    nosleep            ; no -- continue
  281.     btst    #13,d0            ; caller in supervisor mode?
  282.     bne.s    nosleep            ; yes -- don't interrupt
  283.     tst.w    ($43e).w        ; test floppy disk lock variable
  284.     bne.s    nosleep            ; if locked, can't switch
  285. sleep:
  286.     jsr    _preempt        ; does a sleep(READY_Q)
  287. nosleep:
  288.     ori.w    #$0700,sr        ; spl7()
  289.     jsr    _leave_kernel        ; restore vectors
  290.     move.l    _curproc,a0
  291.     pea    4(a0)
  292.     jsr    _restore_context    ; never returns
  293.  
  294. ;
  295. ; we handle errors by calling through to GEMDOS or the BIOS/XBIOS,
  296. ; as appropriate, and letting them handle it -- that way, if the underlying
  297. ; system has functions we don't know about, they still work
  298. ; to figure out which trap we have to call, we use the system call
  299. ; table placed in a5 earlier
  300.  
  301. error:
  302.     cmp.l    #_xbios_tab,a5
  303.     bne.s    maybe_bios
  304.     trap    #14
  305.     bra.s    out
  306. maybe_bios:
  307.     cmp.l    #_dos_tab,a5
  308.     beq.s    trap_1
  309.     trap    #13
  310.     bra.s    out
  311. trap_1:
  312.     trap    #1
  313.     bra.s    out
  314.  
  315. ;
  316. ; sig_return: user signal handlers return to us. At that point, the
  317. ; stack looks like this:
  318. ;    (sp)      (long) signal number -- was a parameter for user routine
  319. ;
  320.     XDEF    _sig_return
  321.     XREF    _valid_return
  322. _sig_return:
  323.     addq.l    #4,sp            ; pop signal number
  324.     move.w    #$11a,-(sp)        ; Psigreturn() system call
  325.     move.w    #1,_valid_return    ; tell kernel it's us!
  326.     trap    #1
  327. ; we had better not come back; if we did, something terrible
  328. ; happened, and we might as well terminate
  329.     move.w    #-998,-(sp)
  330.     move.w    #$4c,-(sp)        ; Pterm()
  331.     trap    #1
  332. ;
  333. ; bconout special code: on entry, a1 points to the stack the user
  334. ; was using. If possible, we just buffer the output until later.
  335. ;
  336.  
  337. do_bconout:
  338.     tst.w    _bconbdev        ; is BIOS buffering on?
  339.     bmi    L_bios            ; no buffering -- skip this code
  340.     move.w    2(a1),d0        ; what device is this for?
  341.     beq    L_bios            ; don't buffer the printer
  342.     cmp.w    _bconbdev,d0        ; same device as is buffered?
  343.     bne.s    new_dev            ; no -- maybe we can't do this
  344. put_buf:
  345.     move.w    4(a1),d0        ; get the character to output
  346.     move.w    _bconbsiz,d1        ; get index into buffer table
  347.     cmp.w    #255,d1            ; buffer full?
  348.     beq    L_bios            ; yes -- flush it out
  349.     lea    _bconbuf,a0
  350.     add.w    d1,a0
  351.     move.b    d0,(a0)            ; store the character
  352.     addq.w    #1,d1
  353.     move.w    d1,_bconbsiz
  354.     jsr    _leave_kernel        ; restore vectors
  355.     moveq.l    #-1,d0            ; return character output OK
  356.     rte
  357.  
  358. new_dev:
  359.     tst.w    _bconbsiz        ; characters already in buffer?
  360.     bne    L_bios            ; yes: we can't buffer this one
  361.     move.w    d0,_bconbdev        ; no: OK, we have a new device
  362.     bra.s    put_buf
  363.  
  364. ;
  365. ; _lineA0: MiNT calls this to get the address of the line A variables
  366. ;
  367.     XDEF    _lineA0
  368. _lineA0:
  369.     movem.l    d2/a2,-(sp)    ; save scratch registers
  370.     dc.w    $a000        ; call the line A initialization routine
  371.     movem.l    (sp)+,d2/a2
  372.     rts
  373.  
  374. ;
  375. ; _call_aes: calls the GEM AES, using the control block passed as
  376. ;            a parameter. Used only for doing appl_init(), to see
  377. ;         if the AES is active yet
  378. ;
  379.     XDEF    _call_aes
  380. _call_aes:
  381.     move.l    4(sp),d1    ; fetch pointer to parameter block
  382.     move.w    #$c8,d0        ; magic number for AES
  383.     movem.l    d2/a2,-(sp)    ; save scratch registers
  384.     trap    #2
  385.     movem.l    (sp)+,d2/a2
  386.     rts
  387.  
  388. ;
  389. ; _callout: Call an external function, passing <32 bytes of arguments,
  390. ; and return the value from the function. NOTE: we must be careful
  391. ; to save all registers here!
  392. ;
  393.     XDEF    _callout
  394.     XDEF    _callout1
  395.     XDEF    _callout2
  396. ;
  397. ; _callout is the general purpose one
  398. ;
  399. _callout:
  400.     lea    8(sp),a0        ; pointer to args
  401.     move.l    4(sp),a1        ; pointer to pointer to function
  402.     movem.l    d2-d7/a2-a6,-(sp)    ; save registers
  403.     movem.l    (a0),d0-d7        ; copy parameters
  404.     lea    -32(sp),sp        ; NOTE: movem.l auto-decrement
  405.     movem.l    d0-d7,(sp)        ;    changes the order of things
  406.     jsr    (a1)            ; go do it
  407.     lea    32(sp),sp
  408.     movem.l    (sp)+,d2-d7/a2-a6    ; restore reggies
  409.     rts
  410. ;
  411. ; _callout2 and _callout1 are for functions with just 1 or
  412. ; 2 16 bit parameters. We cheat and use the same code for
  413. ; both, since passing 32 bits isn't much more expensive than
  414. ; passing 16 bits (and since the called function will just
  415. ; ignore any extra arg)
  416. ;
  417.  
  418. _callout1:
  419. _callout2:
  420.     movem.l    4(sp),a0/a1        ; get function ptr & args
  421.     movem.l    d2-d7/a2-a6,-(sp)    ; save reggies
  422.     move.l    a1,-(sp)        ; push args
  423.     jsr    (a0)            ; do function
  424.     addq.l    #4,sp
  425.     movem.l    (sp)+,d2-d7/a2-a6    ; restore reggies
  426.     rts
  427.  
  428.     END
  429.